package academic.concordia.bankofquebec.entity;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

import academic.concordia.bankofquebec.bean.utils.InvestmentAccountUtils;

@Entity
@DiscriminatorValue(value="INVESTMENT_ACCOUNT")
public class InvestmentAccount extends BankAccount implements Serializable{

	private static final long serialVersionUID = 2L;
	private double annualInterestRate;
	private int periodicity;
	private Date interestPaymentDate;
	private double investedAmount;
	private int timePeriod;
	
	public InvestmentAccount (){
		this.interestPaymentDate = new Date();
	}
	
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}
	
	public void setAnnualInterestRate(double anualInterestRate) {
		this.annualInterestRate = anualInterestRate;
	}
	
	public int getPeriodicity() {
		return periodicity;
	}
	
	public void setPeriodicity(int periodicity) {
		this.periodicity = periodicity;
	}
	
	public Date getInterestPaymentDate() {
		return interestPaymentDate;
	}
	
	public void setInterestPaymentDate(Date interestPaymentDate) {
		this.interestPaymentDate = interestPaymentDate;
	}

	public double getInvestedAmount() {
		return investedAmount;
	}

	public void setInvestedAmount(double investedAmount) {
		this.investedAmount = investedAmount;
	}

	public int getTimePeriod() {
		return timePeriod;
	}

	public void setTimePeriod(int timePeriod) {
		this.timePeriod = timePeriod;
	}
	
	public Transaction depositInterests(double amount){
		Transaction transaction = new Transaction();
		try {
			transaction = this.depositInterests(amount);
			transaction.setTransactionType(TransactionType.INTEREST_PAYMENT);
			calculateInterestPaymentDate();			
		}
		catch (Exception e){
			e.printStackTrace();
		}		
		finally {
			return transaction;
		}
	}
	
	
	public double calculateInterests(){
		double interests = this.getInvestedAmount() * this.getAnnualInterestRate() / 100;
		return interests;
	}
	
	public void calculateInterestPaymentDate(){
		getInterestPaymentDate();
		getPeriodicity();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getInterestPaymentDate());
		calendar.set(getTimePeriod(), calendar.get(getTimePeriod())
								  + getPeriodicity());
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		this.setInterestPaymentDate(calendar.getTime());
	}

}
